#!/bin/bash
#
# control tool for maintaining Kamailio
#
#===================================================================


EGREP="egrep"

## LD_LIBRARY_PATH="$wd":$LD_LIBRARY_PATH

#echo "${BASH_SOURCE[0]}"
#echo "${BASH_SOURCE}"
#echo "$(dirname "${BASH_SOURCE[0]}")"
# echo "$(cd "${dirname BASH_SOURCE[0]}" && pwd)"

FULL_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

OSERBIN="$FULL_DIR/run.console"
PID_FILE="$FULL_DIR/yate.pid"
LOG_FILE="$FULL_DIR/yate.log"

# echo "FULL_DIR $FULL_DIR"

#
##### ------------------------------------------------ #####
### yate_start
#
yate_start() {
	echo
	echo "Starting yate : "
		
	if [ -r $PID_FILE ] ; then
	
		pid_value=`cat $PID_FILE`
		ps_out=`ps -ef | grep yate | grep $pid_value`
		ps_result=$(echo $ps_out | grep "yate")
		
		#echo "Running $ps_result"
		
		if [ "$ps_result" == "" ] ; then
			echo "$pid_value PID file exists ($PID_FILE)! but yate not running!"
		else
			echo "$pid_value PID file exists ($PID_FILE)! yate already running?"
			exit 1
		fi
	fi
	
	if [ ! -x "$OSERBIN" ] ; then
		echo
		echo "yate binaries not found at $OSERBIN"
		echo "set OSERBIN to the path of yate in $0"
		exit 1
	fi
		
	$OSERBIN --there -d -vv -s -r -p $PID_FILE -l $LOG_FILE 

	sleep 3
	
	if [ ! -s $PID_FILE ] ; then
		echo
		echo "PID file $PID_FILE does not exist -- yate start failed"
		exit 1
	fi
	
	echo "started (pid: `cat $PID_FILE`)"
}

#
##### ------------------------------------------------ #####
### yate_stop
#
yate_stop() {
	echo
	if [ -r $PID_FILE ] ; then
		echo "Stopping yate (pid: `cat $PID_FILE`)"
		kill `cat $PID_FILE`
		sleep 3
		echo "stopped"
		rm "$PID_FILE"
	else
		echo
		echo "No PID file found ($PID_FILE)! yate probably not running"
		#echo "check with 'ps axw | $EGREP yate'"
		exit 1
	fi
}

##### ------------------------------------------------ #####
### yate_state
#
yate_state() {

	if [ -r $PID_FILE ] ; then
	
		pid_value=`cat $PID_FILE`
		ps_out=`ps -ef | grep yate | grep $pid_value`
		ps_result=$(echo $ps_out | grep "yate")
		
		#echo "Running $ps_result"
		
		if [ "$ps_result" == "" ] ; then
			echo "stopped, $pid_value PID file exists ($PID_FILE)! but yate not running!"
			exit 1
		else
			echo "running"
			exit 0
		fi
		
	else
		echo "stopped"
		exit 1
	fi
}

usage() {
	
	echo ""
	echo "Existing commands:"
	echo "    -- command 'start|stop|restart|state'"
	echo ""
	echo "    restart .............. restart yate"
	echo "    start ................ start yate"
	echo "    stop ................. stop yate"
	echo "    state ................ state yate"

}

##### ================================================ #####
### main command switch
#
case $1 in

	restart)
		yate_stop
		sleep 2
		yate_start
		;;
	
	start)
		yate_start
		;;

	stop)
		yate_stop
		;;

	state)
		yate_state
		;;

	version)
		echo  "$0 $VERSION"
		;;
		
	*)
		usage
		exit 1
		;;
esac



